/*/
 * 首页逻辑处理的js代码
 * By 张剑锋
/*/

let rec=null
let tag=null
let ipt=null
let btn=null
let pop=null
let data=null

window.onload=()=>{
    rec=document.getElementsByClassName('rec')
    tag=document.getElementById('tag')
    ipt=document.getElementById('ipt')
    btn=document.getElementById('btn')
    pop=document.getElementById('pop')

    addSearchListeners()

    getTestData()/*TODO*/

    Object.keys(data.tag).forEach(e=>addTagElements(tag,e,data.tag[e]))
    
    data.latest.forEach(e=>addBookElements(rec[0],e.id,e.img,e.name,e.writer))
    data.hotest.forEach(e=>addBookElements(rec[1],e.id,e.img,e.name,e.writer))
    data.haopin.forEach(e=>addBookElements(rec[2],e.id,e.img,e.name,e.writer))
    data.tuijian.forEach(e=>addBookElements(rec[3],e.id,e.img,e.name,e.writer))
}

function getTestData(){/*TODO*/
    console.log(data=testData)
}

function addBookElements(par,id,img,name,author){
    let d=document.createElement('div')
    let p=document.createElement('img')
    let n=document.createElement('a')
    let a=document.createElement('a')
    par.appendChild(d)
    d.appendChild(p)
    d.appendChild(n)
    d.appendChild(a)
    p.src=img
    n.href=`详细信息页URL?id=${id}`
    n.innerText=name
    a.innerText=author
}
function addTagElements(par,txt,arr){
    let a=document.createElement('a')
    let d=document.createElement('div')
    par.appendChild(a)
    par.appendChild(d)
    a.innerText=txt
    arr.forEach(e=>{
        let b=document.createElement('button')
        d.appendChild(b)
        b.innerText=e
        b.onclick=()=>{
            window.open(`../srch?tag=${e}`)/*TODO*/
        }
    })
}
function addSearchListeners(){
    let log={key:'',out:true}
    ipt.oninput=()=>{
        let val=ipt.value
        setTimeout(()=>{
            if(val==ipt.value&&val!=log.key)
                if((log.key=val).length)
                    instantSearch(val)
                else
                    hideInstantItems()
        },600)
    }
    ipt.onblur=()=>{
        if(log.out)
            hideInstantItems()
    }
    btn.onclick=()=>{
        let v=ipt.value
        if(v.length)
            clickedSearch(v)
    }
    pop.onmouseenter=()=>log.out=false
    pop.onmouseleave=()=>log.out=true
}
function clickedSearch(s){/*TODO*/
    console.log('ClickedSearch: '+s)
    //跳转到Search页进行搜索并展示
    window.open(`../srch?key=${s}`)
}
function instantSearch(s){/*TODO*/
    console.log('InstantSearch: '+s)

    let data=new FormData()
    let xhr=new XMLHttpRequest()
    data.append('search',s)
    xhr.open('GET','url')
    // xhr.send(data)
    xhr.onload=()=>{
        if(xhr.status==200){}
    }

    showInstantItems(instantItems)
}
function hideInstantItems(){
    showInstantItems([])
}
function showInstantItems(data){
    Array.from(pop.children).forEach(e=>{
        pop.removeChild(e)
    })
    if(data.length){
        data.forEach(v=>{
            let a=document.createElement('div')
            let b=document.createElement('img')
            let c=document.createElement('div')
            let d=document.createElement('a')
            let e=document.createElement('a')
            pop.appendChild(a)
            a.appendChild(b)
            a.appendChild(c)
            c.appendChild(d)
            c.appendChild(e)
            a.onclick=()=>
                location.href=`../srch?key=${v.id}`
            b.src=v.img
            d.innerText=v.name
            e.innerText=v.writer
        })
        pop.style.display='block'
    }else
        pop.style.display='none'
}